package cxm.proj.xtalk;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import cxm.lib.util.MyLog;
import cxm.proj.xtalk.smack.CallManager;
import cxm.proj.xtalk.smack.UserManager;

public class NumberFragment extends Fragment implements OnClickListener {
	private TextView textView = null;
	private ImageButton callButton = null;
	private ImageButton backspaceButton = null;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_number, container, false);
		
		// set grid
		GridView grid = (GridView)view.findViewById(R.id.grid_number);
		grid.setAdapter(new ButtonAdapter());
		// get text view
		this.textView = (TextView)view.findViewById(R.id.number_text);
		this.callButton = (ImageButton)view.findViewById(R.id.number_call);
		this.callButton.setOnClickListener(this);
		this.backspaceButton = (ImageButton)view.findViewById(R.id.number_backspace);
		this.backspaceButton.setOnClickListener(this);
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		this.textView.setText("");
	}

	@Override
	public void onClick(View arg0) {
		if (arg0 == this.callButton) {
			String callName = this.textView.getText().toString();
			if (callName.length() == 0)
				return;

			try {
				CallManager.getInstance().callTo(UserManager.getJidByUserName(callName));
			} catch (Exception e) {
				MyLog.Log(MyLog.Level.Error, "Cannot call to " + callName);
				Toast.makeText(this.getActivity(), "Cannot hold on a call ", Toast.LENGTH_LONG).show();
				return;
			}

			// start call intent to make a call
			Intent intent = new Intent(NumberFragment.this.getActivity(),
					CallActivity.class);
			startActivity(intent);
		} else if (arg0 == this.backspaceButton) {
			String callName = this.textView.getText().toString();
			if (callName.length() > 0)
				this.textView.setText(callName.substring(0, callName.length() - 1));
		} else {
			Button button = (Button)arg0;
			// set number
			this.textView.setText(this.textView.getText().toString() +
					button.getText());
		}
	}
	
	class ButtonAdapter extends BaseAdapter {
	    // private ToggleButton buttons[] = new ToggleButton[8];
	    private Button buttonViews[] = new Button[12];

	    public ButtonAdapter() {
	    	for (int i = 0; i < this.buttonViews.length; i++)
	    		this.buttonViews[i] = null;
	    }

	    public int getCount() {
	        return this.buttonViews.length;
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	if (position < 0 || position >= this.buttonViews.length)
	    		return null;
	    	
	    	if (null == this.buttonViews[position]) {
	    		// inflator view
	    		LayoutInflater inflater = (LayoutInflater)
	    				NumberFragment.this.getActivity().getSystemService(
	    						Context.LAYOUT_INFLATER_SERVICE); 
	    		Button button = (Button)inflater.inflate(R.layout.inf_number, null);
	    		this.buttonViews[position] = button;
	    		
	    		// set text
	    		button.setOnClickListener(NumberFragment.this);
	    		if (position < 9)
		    		button.setText(String.format("%d", position + 1));
	    		else if (position == 9)
	    			button.setText("*");
	    		else if (position == 10)
	    			button.setText("0");
	    		else
	    			button.setText("#");
	    	}
	    	
	    	return this.buttonViews[position];
	    }
	}
}
